package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {
    @Autowired
    private UserService userService;
    @Value("${server.port}")
    int port;
    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id,@RequestHeader(value = "name",required = false) String name) throws InterruptedException {
        if(id.intValue() == 1){
            Thread.sleep(60);
        }else if (id.intValue() == 2){
            throw new RuntimeException("模拟异常");
        }
        System.out.println("请求头 name的值:" + name);
        User user = userService.queryById(id);
        user.setAddress(""+port);
        return user;
    }

//    @Value("${pattern.dateformat}")
//    private String dateformat;
    @Autowired
    PatternProperties patternProperties;

    @GetMapping("now")
    public String now(){
        //  获取当前时间     按照配置中的格式  进行时间格式化
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    @GetMapping("prop")
    public PatternProperties prop(){
        return patternProperties;
    }

    @PostMapping("save")
    public String save(@RequestBody User user){
        System.out.println(user);
        return "保存成功了";
    }
}
